package com.vvv.designpattern.dutychain;

import com.vvv.designpattern.pojo.BusinessLaunch;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author vvv
 * @date 2023-11-24 10 22
 * discription责任链模式：XXXHandler为具体责任角色，
 */
public class SexHandler extends AbstractBusinessHandler{
    @Override
    public List<BusinessLaunch> processHandler(List<BusinessLaunch> launchList
            , String targetCity, String targetSex, String targetProduct) {
        // 1. 如果launchList为null直接返回
        if (launchList.isEmpty()) {
            return launchList;
        }
        // 2. 根据sex进行筛选
        launchList = launchList.stream().filter(launch -> {
            String sex = launch.getTargetSex();
            // 该业务投放不限制城市
            if (sex.isEmpty()) {
                return true;
            }
            List<String> sexList = Arrays.asList(sex.split(","));
            return sexList.contains(targetSex);
        }).collect(Collectors.toList());
        // 3. 如果hasNextHandler则调用下一个责任节点的processHandler
        if (hasNextHandler()) {
            return nextHandler.processHandler(launchList, targetCity, targetSex, targetProduct);
        }
        // 4. 通一返回launchList，因为不确定那个责任节点是最后一个
        return launchList;
    }
}
